In [17]:
%matplotlib inline

from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap

import matplotlib.pyplot as plt

import plotly as py
py.offline.init_notebook_mode()
import pandas as pd

import colorlover as cl
from IPython.display import HTML, display

import vincent
vincent.core.initialize_notebook()



In [2]:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')

In [3]:
def get_string(x):
    return "<br>".join("%s: %s" % (k.title(), x[k]) for k in [
            "state", "beef", "total fruits",
            "total veggies", "wheat", "corn"
        ])

In [4]:
df.head().apply(get_string, axis=1)


Out[4]:
0    State: Alabama<br>Beef: 34.4<br>Total Fruits: ...
1    State: Alaska<br>Beef: 0.2<br>Total Fruits: 0....
2    State: Arizona<br>Beef: 71.3<br>Total Fruits: ...
3    State: Arkansas<br>Beef: 53.2<br>Total Fruits:...
4    State:  California<br>Beef: 228.7<br>Total Fru...
dtype: object

In [5]:
for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
df["text"] = df.apply(get_string, axis=1)

In [6]:
df.head()


Out[6]:
code state category total exports beef pork poultry dairy fruits fresh fruits proc total fruits veggies fresh veggies proc total veggies corn wheat cotton text
0 AL Alabama state 1390.63 34.4 10.6 481.0 4.06 8.0 17.1 25.11 5.5 8.9 14.33 34.9 70.0 317.61 State: Alabama<br>Beef: 34.4<br>Total Fruits: ...
1 AK Alaska state 13.31 0.2 0.1 0.0 0.19 0.0 0.0 0.0 0.6 1.0 1.56 0.0 0.0 0.0 State: Alaska<br>Beef: 0.2<br>Total Fruits: 0....
2 AZ Arizona state 1463.17 71.3 17.9 0.0 105.48 19.3 41.0 60.27 147.5 239.4 386.91 7.3 48.7 423.95 State: Arizona<br>Beef: 71.3<br>Total Fruits: ...
3 AR Arkansas state 3586.02 53.2 29.4 562.9 3.53 2.2 4.7 6.88 4.4 7.1 11.45 69.5 114.5 665.44 State: Arkansas<br>Beef: 53.2<br>Total Fruits:...
4 CA California state 16472.88 228.7 11.1 225.4 929.95 2791.8 5944.6 8736.4 803.2 1303.5 2106.79 34.6 249.3 1064.95 State: California<br>Beef: 228.7<br>Total Fru...

In [7]:
data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['total exports'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "Millions USD")
        ) ]

layout = dict(
        title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showlakes = True,
            lakecolor = 'rgb(255, 255, 255)'),
             )

In [8]:
fig = dict( data=data, layout=layout )
py.offline.iplot( fig, filename='d3-cloropleth-map' )


Using matplotlib


In [10]:
df.head()


Out[10]:
code state category total exports beef pork poultry dairy fruits fresh fruits proc total fruits veggies fresh veggies proc total veggies corn wheat cotton text
0 AL Alabama state 1390.63 34.4 10.6 481.0 4.06 8.0 17.1 25.11 5.5 8.9 14.33 34.9 70.0 317.61 State: Alabama<br>Beef: 34.4<br>Total Fruits: ...
1 AK Alaska state 13.31 0.2 0.1 0.0 0.19 0.0 0.0 0.0 0.6 1.0 1.56 0.0 0.0 0.0 State: Alaska<br>Beef: 0.2<br>Total Fruits: 0....
2 AZ Arizona state 1463.17 71.3 17.9 0.0 105.48 19.3 41.0 60.27 147.5 239.4 386.91 7.3 48.7 423.95 State: Arizona<br>Beef: 71.3<br>Total Fruits: ...
3 AR Arkansas state 3586.02 53.2 29.4 562.9 3.53 2.2 4.7 6.88 4.4 7.1 11.45 69.5 114.5 665.44 State: Arkansas<br>Beef: 53.2<br>Total Fruits:...
4 CA California state 16472.88 228.7 11.1 225.4 929.95 2791.8 5944.6 8736.4 803.2 1303.5 2106.79 34.6 249.3 1064.95 State: California<br>Beef: 228.7<br>Total Fru...

In [20]:
world_topo = r'world-countries.topo.json'
geo_data = [{'name': 'countries',
             'url': world_topo,
             'feature': 'world-countries'}]

vis = vincent.Map(geo_data=geo_data, scale=200)
display(vis)



In [ ]: